// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

import 'common/stylus/index.styl'

import axios from 'axios'

import getTextByLangKey from "./common/lang";

//Element 的引入
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

// import lang from 'element-ui/lib/locale/lang/en'
import enlocale from 'element-ui/lib/locale/lang/en'
import zhlocale from 'element-ui/lib/locale/lang/zh-CN'
// import locale from 'element-ui/lib/locale'

import language from "./common/lang/utils";
// 设置语言
// locale.use(lang)

// const getUrlParam = function (url, key) {
//   const urlSplit = url.split('?')
//   const arr = urlSplit[1] ? urlSplit[1].split('&') : []
//   const params = {}
//   for (let i = 0; i < arr.length; i++) {
//     const data = arr[i].split('=')
//     if (data.length === 2) {
//       params[data[0]] = data[1]
//     }
//   }
//   return params[key]
// }
//
//
// const urlLang = getUrlParam(window.location.href, 'lang')
// let language
// if(urlLang){
//   localStorage.setItem("language", urlLang)
//   language = urlLang
// }else{
//   language = localStorage.getItem("language") || 'zh'
// }


// console.log('localStorage.getItem("language")', localStorage.getItem("language"))

import util from '@/common/js/util'
import request from '@/common/js/request'

if (language === 'zh' || 'zh_CN') {
  Vue.use(ElementUI, {locale: zhlocale})
}
if (language === 'en') {
  Vue.use(ElementUI, {locale: enlocale})
}


Vue.config.productionTip = false

Vue.prototype.$wlsUtil = util
Vue.prototype.$wlsRequset = request
Vue.prototype.$getTextByLangKey = getTextByLangKey(language)

Date.prototype.format = function (fmt) { //author: meizz
  var o = {
    "M+": this.getMonth() + 1,                 //月份
    "d+": this.getDate(),                    //日
    "h+": this.getHours(),                   //小时
    "m+": this.getMinutes(),                 //分
    "s+": this.getSeconds(),                 //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds()             //毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          // 返回 401 清除token信息并跳转到登录页面
          // console.log('-ssfsdfsfdsf骨过期')
          // console.log(this)
          // console.log(Vue)
          const redirectArr = window.location.href.split('#')
          if(window.location.href.indexOf('login')!==-1){
            //已经在登录了
            return
          }
          if(redirectArr.length>=2){
            const redirect = redirectArr[1]
            app.$wlsUtil.linkToPath(`/login?redirect=${redirect}`)
          }else{
            app.$wlsUtil.linkToPath(`/login`)
          }
          // // console.log('redirect',redirect)
          // console.log('redirect',window.location.href)
          // console.log(' redirect:encodeURIComponent(this.$route.path)',window.location)
          // redirect:encodeURIComponent(this.$route.path)
          // app.$wlsUtil.linkToPath('/login')

        // confirm('过期')
        // router.replace({
        //   path: '/login'
        // })
        // location.reload()
      }
    }
    return Promise.reject(error)   // 返回接口返回的错误信息
  })


/* eslint-disable no-new */
let app = new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: {App}
})

export {
  app, axios
}
